<?php
namespace app\admin\controller;

use app\model\Carmine as CarmineModel;
use app\model\CarmineData as CarmineDataModel;

class CarmineController extends Base
{

    public function index()
    {
        $page = input('post.page', 1);
        $limit = input('post.limit', 10);
        $offset = ($page - 1) * $limit;
        $where = ['status'=>1];
        $CarmineModel = new CarmineModel();
        $item = $CarmineModel->where($where)->limit($offset, $limit)->order('id desc')->select();
        $count = $CarmineModel->where($where)->count();
        foreach ($item as $k => $v) {
            $item[$k]['term_time'] = $v['term_time'] == 0 ? '长期有效' : date('Y-m-d H:i:s', $v['term_time']);
            $item[$k]['type'] = $v['type'] == 1 ? '积分' : ($v['type'] == 2 ? '余额' : '') ;
        }
        return $this->json(['code' => 0, 'msg' => '', 'data' => ['count' => $count, 'item' => $item]]);
    }


    public function create()
    {
        $number = input('post.number', 0);
        $quota = input('post.quota', 0);
        $type = input('post.type', 0);
        $CarmineModel = new CarmineModel();
        $count = $CarmineModel->whereTime('create_time',  date('Y-m-d'))->count();
        $data = [
            'title' => date('YmdHis'). str_pad($count+1, 3, '0', STR_PAD_LEFT),
            'weid' => weid(),
            'number' => $number,
            'quota' => $quota,
            'type' => $type,
        ];
        $term_time = input('post.term_time');
        if(!empty($term_time)){
            $data['term_time'] = strtotime($term_time);
        }
        $CarmineModel->save($data);
        $carmine_id = $CarmineModel->id;
        $random_code = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
        $this->MakeCard($number, weid() . "_" . $random_code, date('Ymd'), $carmine_id);
        return $this->json(['code' => 0, 'msg' => '创建成功']);

//        $this->MakeCard($number, $mid . "_" . $random_code, date('Ymd'));
    }

    public function data()
    {
        $cid = input('post.cid', 0);
        if ($cid > 0) {
            $limit = input('post.limit', 10);
            $page = input('post.page', 1);
            $offset = ($page - 1) * $limit;
            $CarmineDataModel = new CarmineDataModel();
            $map = ['status'=>1, 'cid'=>$cid];
            $item = $CarmineDataModel->where($map)->limit($offset, $limit)->order('id desc')->select();
            $count = $CarmineDataModel->where($map)->count();
            foreach ($item as $k => $v) {
                $item[$k]['state'] = $v['state'] == 0 ? '未使用' : '已使用';
            }
            return $this->json(['code' => 0, 'msg' => '', 'data' => ['count' => $count, 'item' => $item]]);
        }
    }

    public function delete()
    {
        $id = input('post.id', 0);
        if ($id > 0) {
            $CarmineModel = new CarmineModel();
            $CarmineDataModel = new CarmineDataModel();
            $CarmineModel->where('id', $id)->update(['status'=>0]);
            $CarmineDataModel->where('cid', $id)->update(['status'=>0]);
            return $this->json(['code' => 0, 'msg' => '删除成功']);
        }
    }

    /**
     * @param $num 数量
     * @param $point 点数
     * @param $batch 批号
     * @param $ym 发行年月
     * @return float|int|void
     */
    private function MakeCard($num, $batch, $ym, $carmine_id)
    {
        set_time_limit(0);
        //处理缓冲区
        ob_end_clean();
        ob_implicit_flush(true);
        if ($num == 0) return;
        $CarmineDataModel = new CarmineDataModel();
        for ($i = 1; $i <= $num; $i++) {
            $sn = sprintf("%02s%s%06s", $batch, $ym, $i);
            $seek = mt_rand(0, 9999) . mt_rand(0, 9999) . mt_rand(0, 9999); //12位
            $start = mt_rand(0, 20);
            $str = strtoupper(substr(md5($seek), $start, 12));
            $str = str_replace("O", chr(mt_rand(65, 78)), $str);
            $str = str_replace("0", chr(mt_rand(65, 78)), $str);
//            $row = array('sn' => $sn, 'password' => $str, 'created' => time());
            //查重
            //在这里加插入数据的代码.

            $CarmineDataModel->insert([
                'weid' => weid(),
                'cid' => $carmine_id,
                'sn' => $sn,
                'password' => $str,
                'state' => 0,
                'status' => 1,
                'create_time' => time(),
                'update_time' => time(),
            ]);
        }
        return $num;

    } //函数结束








}
?>

